package com.yitao.cms.service.product;

import com.skipper.base.bean.CommonStatus;
import com.skipper.base.bean.DataOutput;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.product.BaseProductCategoryDto;
import com.yitao.client.model.product.BaseProductCategoryModel;
import com.yitao.client.model.product.QBaseProductCategoryModel;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseProductCategoryService extends GenericEntityService<BaseProductCategoryModel, BaseProductCategoryDto, QBaseProductCategoryModel> {
    public BaseProductCategoryService(EntityManager entityManager) {
        super(entityManager);
    }

    /**
     * 根据父ID查询子及分类
     *
     * @param parentId
     * @return
     */
    public DataOutput findBaseCategoryByParentId(Long parentId) {
        if (parentId != null) {
            List<BaseProductCategoryDto> baseProductCategoryDtoList = repository.findAll(Q.parentId.eq(parentId).and(Q.isDelete.eq(0)))
                    .stream().map(this::convertEntityToDto).collect(Collectors.toList());
            return new DataOutput(baseProductCategoryDtoList);
        } else {
            return new DataOutput(CommonStatus.BAD_REQUEST);
        }

    }

    /**
     * 查询基础分类父级Id
     *
     * @param
     * @return
     */
    public Long findParentCategoryId(Long categoryId) {
        Long parentId = null;
        if (categoryId != null) {
            parentId = queryFactory.select(Q.parentId).from(Q).where(Q.id.eq(categoryId)).fetchFirst();

        }
        return parentId;
    }

    /**
     * 根据基础分类id查询基础分类数据
     *
     * @param Id
     * @return
     */
    public BaseProductCategoryModel BaseProductCategoryById(Long Id) {
        if (Id != null) {
            return queryFactory.select(Q).from(Q).where(Q.id.eq(Id)).fetchFirst();
        }
        return null;
    }

    /**
     * 查询全部的一级分类
     *
     * @return
     */
    public List<BaseProductCategoryModel> getBaseProductCategoryModelList() {
        return queryFactory.select(Q).from(Q).where(Q.parentId.eq(0L)).fetch();
    }


}
